package com.ruoyi.work.service.impl;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.work.domain.WorkOfficeType;
import com.ruoyi.work.mapper.WorkOfficeTypeMapper;
import com.ruoyi.work.service.IWorkOfficeTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: MMDZ
 * @Desc:
 */
@Service
public class WorkOfficeTypeServiceImpl implements IWorkOfficeTypeService {

    @Autowired
    private WorkOfficeTypeMapper workOfficeTypeMapper;

    /**
     * 查询所有事务种类列表 - 条件 分页
     *
     * @param workOfficeType
     * @return
     */
    @Override
    public List<WorkOfficeType> selectWorkOfficeTypeList(WorkOfficeType workOfficeType) {
        return workOfficeTypeMapper.selectWorkOfficeTypeList(workOfficeType);
    }

    /**
     * 查询所有事务种类列表
     *
     * @return
     */
    @Override
    public List<WorkOfficeType> selectOfficeTypeAll() {
        return workOfficeTypeMapper.selectOfficeTypeAll();
    }

    /**
     * 根据id查询事务种类详情
     *
     * @param officeId
     * @return
     */
    @Override
    public WorkOfficeType selectWorkOfficeTypeById(Long officeId) {
        return workOfficeTypeMapper.selectWorkOfficeTypeById(officeId);
    }

    /**
     * 事务种类名称是否重复
     *
     * @param workOfficeType
     * @return
     */
    @Override
    public String checkOfficeNameUnique(WorkOfficeType workOfficeType) {
        Long officeId = StringUtils.isNull(workOfficeType.getOfficeId()) ? -1L : workOfficeType.getOfficeId();
        WorkOfficeType info = workOfficeTypeMapper.checkOfficeNameUnique(workOfficeType.getOfficeName());
        if (StringUtils.isNotNull(info) && info.getOfficeId().longValue() != officeId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 新增
     *
     * @param workOfficeType
     * @return
     */
    @Override
    public int insertWorkOfficeType(WorkOfficeType workOfficeType) {
        return workOfficeTypeMapper.insertWorkOfficeType(workOfficeType);
    }

    /**
     * 修改
     *
     * @param workOfficeType
     * @return
     */
    @Override
    public int updateWorkOfficeType(WorkOfficeType workOfficeType) {
        return workOfficeTypeMapper.updateWorkOfficeType(workOfficeType);
    }

    /**
     * 删除
     *
     * @param officeIds
     * @return
     */
    @Override
    public int deleteWorkOfficeTypeByIds(Long[] officeIds) {
        for (Long officeId : officeIds) {
            WorkOfficeType officeType = selectWorkOfficeTypeById(officeId);
            if (countFillOfficeById(officeId) > 0) {
                throw new ServiceException(String.format("%1$s已分配,不能删除", officeType.getOfficeName()));
            }
        }
        return workOfficeTypeMapper.deleteWorkOfficeTypeByIds(officeIds);
    }

    /**
     * 通过事务ID查询工时填报使用数量
     *
     * @param officeId 事务种类ID
     * @return 结果
     */
    @Override
    public int countFillOfficeById(Long officeId) {
        return workOfficeTypeMapper.countFillOfficeById(officeId);
    }
}
